package com.powergisol.gis.service.impl;

import com.powergisol.gis.dao.TabServiceMapper;
import com.powergisol.gis.dao.TabStyleMapper;
import com.powergisol.gis.entity.TabStyle;
import com.powergisol.gis.service.TabStyleService;
import com.powergisol.gis.util.PropertyUtil;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.TabStyleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Date: 2019/7/26  16:14
 * Description:
 */
@Service
public class TabStyleServiceImpl implements TabStyleService {
    @Autowired
    private TabStyleMapper tabStyleMapper;
    @Autowired
    private TabServiceMapper tabServiceMapper;
//    @Value("${style-url}")
//    private String styleUrl;

    @Override
    public List<TabStyleVo> tabStyleList(TabStyleVo vo) {
        return tabStyleMapper.tabStyleList(vo);
    }

    @Override
    public Result delTabStyleById(Integer id) {
        tabStyleMapper.deleteByPrimaryKey(id);
        return new Result().success("删除成功");
    }

    @Override
    public Result getTabStyle(Integer id) {
        TabStyle tabStyle =  tabStyleMapper.selectByPrimaryKey(id);
        return new Result().success(tabStyle);
    }

    @Override
    public Result saveTabStyle(TabStyleVo vo) {
        Result result = new Result();
        String userDir = System.getProperty("user.dir");
        userDir = userDir.substring(0,userDir.lastIndexOf(File.separator));
        String filePath = userDir+File.separator+"serviceData"+File.separator+"style"+File.separator+vo.getStyleName()+".sld";
        try {
            writeFile(filePath,vo.getStyleContentOrigin());
        } catch (IOException e) {
            e.printStackTrace();
            return result.failure(-1,e.getMessage());
        }
        vo.setStyleUrl(vo.getStyleName()+".sld");
        vo.setCreateTime(new Date());
        tabStyleMapper.insertSelective(vo);
        return result.success("保存成功");
    }

    private void writeFile(String filePath,String content) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        }
    }

    @Override
    public Result upTabStyle(TabStyleVo vo) {
        Result result = new Result();
        String userDir = System.getProperty("user.dir");
        userDir = userDir.substring(0,userDir.lastIndexOf(File.separator));
        String filePath = userDir+File.separator+"serviceData"+File.separator+"style"+File.separator+vo.getStyleUrl();
        try {
            upFile(filePath,vo.getStyleContentOrigin());
        } catch (IOException e) {
            e.printStackTrace();
            return result.failure(-1,e.getMessage());
        }
        tabStyleMapper.updateByPrimaryKeySelective(vo);
        return result.success("保存成功");
    }

    private void upFile(String filePath,String content) throws IOException {
        File file = new File(filePath);
        file.delete();
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        }
    }

    @Override
    public Integer isStyleNameDouble(String styleName) {
        return tabStyleMapper.isStyleNameDouble(styleName);
    }
}
